 import { getArticleLikes} from '@/services';
import { IArticleLikes,} from '@/types';
import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';

export interface LikeModelState {
  data:Partial<IArticleLikes>,
}

export interface LikeModelType {
  namespace: 'likes';
  state: LikeModelState;
  effects: {
    getArticleLikes:Effect;
  };
  reducers: {
    save: Reducer<LikeModelState>;
  };
}

const LikeModel: LikeModelType = {
  // 数据模型，用来隔离数据防止相互影响
  namespace: 'likes',

  // 数据模型状态，相当于vuex的state
  state: {
    data:{}
  },

  // 异步操作，相当于vuex的action
  effects: {
    *getArticleLikes({ payload }, { call, put }) {
      let result = yield call(getArticleLikes, payload.id,payload.type,payload.path);
       if (result.data) {
        yield put({
          type: 'save',
          payload: {
            data: result.data
          }
        })
      }
    },
  },

  // 同步操作，相当于vuex的mutation
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },

  }
};

export default LikeModel;

